/**
 * 转账流程状态
 */
export const state = () => {
  let account = sessionStorage.getItem("pay-accept-account")||'';
  let userInfo = sessionStorage.getItem("pay-accept-user-info");
  let form = sessionStorage.getItem("pay-accept-form");
  let status = sessionStorage.getItem("pay-accept-status");

  try {
    userInfo = JSON.parse(userInfo);
  } catch (e) {
    userInfo = null;
  }

  try {
    form = JSON.parse(form);
  } catch (e) {
    form = null;
  }

  try {
    status = JSON.parse(status);
  } catch (e) {
    status = null;
  }
  return {
    status,
    account,
    userInfo,
    form, //转账表单
    wallets:[],// 钱包币种类型
  }
}

export const mutations = {
  setStatus(state, status) {
    if (status) {
      sessionStorage.setItem("pay-accept-status", JSON.stringify(status));
    } else {
      sessionStorage.removeItem("pay-accept-status");
    }
    state.status = status || null
  },
  setAccount(state, account) {
    if (account) {
      sessionStorage.setItem("pay-accept-account", account);
    } else {
      sessionStorage.removeItem("pay-accept-account");
    }
    state.account = account;
  },
  setUserInfo(state, user) {
    // 每次保存后缓存到本地
    if (user) {
      sessionStorage.setItem("pay-accept-user-info", JSON.stringify(user));
    } else {
      sessionStorage.removeItem("pay-accept-user-info");
    }
    state.userInfo = user || null
  },
  setForm(state, form) {
    if (form) {
      sessionStorage.setItem("pay-accept-form", JSON.stringify(form));
    } else {
      sessionStorage.removeItem("pay-accept-form");
    }
    state.form = form || null
  },
  setWallets(state, wallets) {
    state.wallets = wallets || []
  },

}

export const actions = {
  clear({
    commit
  }) { // 清楚所有状态
    commit('setStatus', null)
    commit('setAccount', '')
    commit('setUserInfo', null)
    commit('setForm', null)
  },
  async checkWallets({commit}){
    try{
      let {data:rs} = await this.$axios.post('?ct=api&ac=check_wallets');
      if(rs.code == 0){
        commit('setWallets',rs.data.wallets)
      }else{
        this.$toast(rs.msg);
      }
    }catch(e){
      this.$toast('获取钱包数据失败');
      this.$error(e);
    }

  }
}
